#!/bin/bash

STR=`head -n +1 ./chainArgs.txt`
BLOCKCHAIN_NAME=($STR) #bupt
STR=`sed -n '2p' ./chainArgs.txt` 
FIRST_ORG_NAME=($STR)  #org1
STR=`sed -n '3p' ./chainArgs.txt`
ORDERER_URL=($STR)     #
STR=`sed -n '4p' ./chainArgs.txt`
FIRST_ORG_PEER_URL=($STR)
STR=`sed -n '5p' ./chainArgs.txt`
CRYPTO_PATH=($STR)

function genInitFile(){
	# echo "-------------------------------"
	# for(( i=0;i<${#ORDERER
	# echo -e "${ORDERER_URL[i]} order\n"
	# done; 
	# echo "-------------------------------"
	# echo -e "联盟名：${BLOCKCHAIN_NAME} 初始组织名：${FIRST_ORG_NAME} 路径:${CRYPTO_PATH}\n "
	# echo "-------------------------------"
	# for(( i=0;i<${#FIRST_ORG_PEER_URL[*]};i++)) do
	# echo -e "${FIRST_ORG_PEER_URL[i]} peer\n"
	# done; 
	# echo "-------------------------------"
	echo "# ${BLOCKCHAIN_NAME}
# ${FIRST_ORG_NAME}
# ${CRYPTO_PATH} 
# ${#ORDERER_URL[*]}
version: 1.0.0

client:
  organization: ${FIRST_ORG_NAME} #第一个组织名
  logging:
    level: info
  cryptoconfig:
    path:  #crypto-config
  credentialStore:
    path: \"/tmp/cpsecstore\"
  tlsCerts:
    systemCertPool: true
    client:
      #crypto-config/...
      keyfile: ${CRYPTO_PATH}/peerOrganizations/${FIRST_ORG_NAME}/users/Admin@${FIRST_ORG_NAME}/tls/client.key
      certfile: ${CRYPTO_PATH}/peerOrganizations/${FIRST_ORG_NAME}/users/Admin@${FIRST_ORG_NAME}/tls/client.cert

channels:

organizations:
  ${FIRST_ORG_NAME}: #第一个组织名 #TAGORGS:_${FIRST_ORG_NAME}
    mspid: ${FIRST_ORG_NAME} #第一个组织名 #TAGORGS:_${FIRST_ORG_NAME}

    cryptoPath: ${CRYPTO_PATH}/peerOrganizations/${FIRST_ORG_NAME}/users/Admin@${FIRST_ORG_NAME}/msp #TAGORGS:_${FIRST_ORG_NAME}

    peers:    #${FIRST_ORG_NAME} #TAGORGS:_${FIRST_ORG_NAME}" 

    for(( i=0;i<${#FIRST_ORG_PEER_URL[*]};i++)) do
	echo "      - peer${i}-${FIRST_ORG_NAME} #TAGORGS:_${FIRST_ORG_NAME}"
	done; 
	echo "  ordererorg:
    mspID: OrdererMSP
    # crypto-config/ordererOrganizations/联盟名.com/users/Admin@联盟名.com/msp"
    echo "    cryptoPath: ${CRYPTO_PATH}/ordererOrganizations/${BLOCKCHAIN_NAME}/users/Admin@${BLOCKCHAIN_NAME}/msp"

    echo "orderers:
  #order数组"
    for(( i=0;i<${#ORDERER_URL[*]};i++)) do
	echo "  orderer${i}:
    # order[i] url
    url: ${ORDERER_URL[i]}

    grpcOptions:
      ssl-target-name-override: orderer${i}
      keep-alive-time: 0s
      keep-alive-timeout: 20s
      keep-alive-permit: false
      fail-fast: false

      allow-insecure: false


    tlsCACerts:
      # crypto-config/ordererOrganizations/联盟名.com/tlsca/tlsca.联盟名.com-cert.pem
      path: ${CRYPTO_PATH}/ordererOrganizations/${BLOCKCHAIN_NAME}/tlsca/tlsca.${BLOCKCHAIN_NAME}-cert.pem"
	done; 
	echo "peers:
  #peer数组 peer[i].第一个组织名.联盟名.com #TAGPEERS:_${FIRST_ORG_NAME}" 
      for(( i=0;i<${#FIRST_ORG_PEER_URL[*]};i++)) do
	echo "  peer${i}-${FIRST_ORG_NAME}: #TAGPEERS:_${FIRST_ORG_NAME}
    #peer数组 peer[i].第一个组织名.联盟名.com #TAGPEERS:_${FIRST_ORG_NAME}
    url: ${FIRST_ORG_PEER_URL[i]} #TAGPEERS:_${FIRST_ORG_NAME}

    grpcOptions: #TAGPEERS:_${FIRST_ORG_NAME}
      # peer[i].${FIRST_ORG_NAME}.联盟名.com #TAGPEERS:_${FIRST_ORG_NAME}
      ssl-target-name-override: peer${i}-${FIRST_ORG_NAME} #TAGPEERS:_${FIRST_ORG_NAME}
      keep-alive-time: 0s #TAGPEERS:_${FIRST_ORG_NAME}
      keep-alive-timeout: 20s #TAGPEERS:_${FIRST_ORG_NAME}
      keep-alive-permit: false #TAGPEERS:_${FIRST_ORG_NAME}
      fail-fast: false #TAGPEERS:_${FIRST_ORG_NAME}
      allow-insecure: false #TAGPEERS:_${FIRST_ORG_NAME}

    tlsCACerts: #TAGPEERS:_${FIRST_ORG_NAME}
      # crypto-config/peerOrganizations/第一个组织名.联盟名.com/tlsca/tlsca.第一个组织名.组织名.com-cert.pem #TAGPEERS:_${FIRST_ORG_NAME}
      path: ${CRYPTO_PATH}/peerOrganizations/${FIRST_ORG_NAME}/tlsca/tlsca.${FIRST_ORG_NAME}-cert.pem #TAGPEERS:_${FIRST_ORG_NAME}"
	done; 
}

genInitFile > ./config_${BLOCKCHAIN_NAME}.yaml
